//
//  SerachQuestionAndAnswerVC.m
//  OnionHome
//
//  Created by fengjie on 2019/9/23.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "SerachQuestionAndAnswerVC.h"

@interface SerachQuestionAndAnswerVC ()<UITableViewDelegate,UITableViewDataSource>

/**搜索视图*/
@property (nonatomic,strong)UITextField*serachField;
/**<#属性#>*/
@property (nonatomic,strong)UIButton * rightBUtton;

/**listView*/
@property (nonatomic,strong)UITableView*listView;
/**数据源*/
@property (nonatomic,strong)NSMutableArray*dataArray;
@end

@implementation SerachQuestionAndAnswerVC

- (void)viewDidLoad{
    [super viewDidLoad];
    
    [self setSerachView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setIsHidenNaviBar:YES];
}
#pragma mark --UITableViewDelegate,UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SerachQuestionAndAnswerCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SerachQuestionAndAnswerCell class])];
    
    return cell;
}
-(UITableView*)listView{
    if(!_listView){
        _listView=[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _listView.delegate= self;
        _listView.dataSource = self;
        _listView.rowHeight = UITableViewAutomaticDimension;
        _listView.estimatedRowHeight = 44.0f;

        [_listView registerClass:[SerachQuestionAndAnswerCell class] forCellReuseIdentifier:NSStringFromClass([SerachQuestionAndAnswerCell class])];
    }
    return _listView;
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray =[NSMutableArray array];
    }
    return _dataArray;
}
/**
 设置搜索栏
 */
-(void)setSerachView{
    UIView * view = [self createSerbar];
    
    [self.view addSubview:self.listView];
    

    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(K_StatusBarHeight);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];
    
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(view.mas_bottom);
        make.left.bottom.right.mas_equalTo(0);
    }];

}
-(UIView * )createSerbar{
    UIView * view = UIView.new;
    view.backgroundColor = UIColor.whiteColor;
    
    UIView * view1 = UIView.new;
    view1.backgroundColor = UIColorFromRGB(0xF5F7F5);
    ViewRadius(view1, 4);
    
    [view addSubview:view1];
    self.rightBUtton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.rightBUtton setTitle:@"取消" forState:UIControlStateNormal];
    [self.rightBUtton setTitleColor:UIColorFromRGB(0x273323) forState:UIControlStateNormal];
    self.rightBUtton.titleLabel.font = SYSTEMFONT(16);
    [view addSubview:self.rightBUtton];
    
    kWeakSelf(weakSelf);
    [self.rightBUtton addActionHandler:^{
        [weakSelf backBtnClicked];
    }];
    
    
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-64);
        make.height.mas_equalTo(30);
    }];
    
    UIImageView * leftImage = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"ic_nav_search")];
    [view1 addSubview:leftImage];
    [view1 addSubview:self.serachField];
    
    
    [leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(8);
        make.width.height.mas_equalTo(18);
    }];
    
    [self.serachField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(leftImage.mas_right).offset(4);
        make.right.mas_equalTo(self.rightBUtton.mas_left).offset(-16);
        make.top.bottom.mas_equalTo(0);
    }];
    [self.rightBUtton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(40);
        make.centerY.mas_equalTo(0);
    }];
    
    
    return view;
}

-(UITextField*)serachField{
    if(!_serachField){
        _serachField=[UITextField new];
        _serachField.placeholder = @"请输入问题关键词";
        _serachField.userInteractionEnabled = YES;
        _serachField.clearButtonMode =UITextFieldViewModeAlways;
    }
    return _serachField;
}
-(UIView * )createHeaderTilteViewLeftName:(NSString *)leftName rightImage:(NSString *)rightImage rightName:(NSString *)rightName{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0,  KScreenWidth-32, 50)];
    
    UILabel * leftLabel = UILabel.new;
    leftLabel.text = leftName;
    leftLabel.font = SYSTEMFONT(14);
    leftLabel.textColor = UIColorFromRGB(0x454F44);
    
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    if (rightImage == nil) {
        [rightBtn setTitle:rightName forState:UIControlStateNormal];
        [rightBtn setTitleColor:UIColorFromRGB(0x959E97) forState:UIControlStateNormal];
        rightBtn.titleLabel.font = SYSTEMFONT(11);

    }else{
        [rightBtn setBackgroundImage:IMAGE_NAMED(rightImage) forState:UIControlStateNormal];
    }
    
    [view addSubview:leftLabel];
    [view addSubview:rightBtn];
    
    [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.centerY.mas_equalTo(0);
    }];
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.centerY.mas_equalTo(0);
    }];

    return view;
}
@end


@implementation SerachQuestionAndAnswerCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createUI];
    }
    
    return self;
}
-(void)createUI{
    UIImageView * leftImage = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"ic_nav_search")];
    
    UILabel * contenxLabel = UILabel.new;
    contenxLabel.textColor = UIColorFromRGB(0x273323);
    contenxLabel.font = SYSTEMFONT(16);
    contenxLabel.numberOfLines = 0;
    contenxLabel.text = @"作为24岁，100斤的成年女性，每天可以吃几个鸡蛋";
    
    UILabel * rightLabel = UILabel.new;
    rightLabel.textColor = UIColorFromRGB(0x454F44);
    rightLabel.font = SYSTEMFONT(12);
    rightLabel.text = @"112个回答";
    
    [self addSubview:leftImage];
    [self addSubview:contenxLabel];
    [self addSubview:rightLabel];
    
    [leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.centerY.mas_equalTo(0);
        make.height.width.mas_equalTo(16);
    }];
    [contenxLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(16);
        make.left.mas_equalTo(leftImage.mas_right).offset(12);
        make.bottom.mas_equalTo(-16);
        make.right.mas_equalTo(-90);
    }];
    [rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
        make.centerY.mas_equalTo(0);
    }];

}
@end
